﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class KhoAdd : System.Web.UI.Page
    {
        IUnitOfWork iunitfwork = new EFUnitOfWork();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadHienthi();
                LoadNhanVien();
            }
        }
        public void LoadHienthi()
        {
            string strid = "" + Request["MAKHO"];
            KHORepository repository = new KHORepository(new EFRepository<KHO>(), iunitfwork);
            KHO objKho = null;
            if (!string.IsNullOrEmpty(strid))
            {
                objKho = repository.GetById(strid);
                txtmakho.Value = objKho.MAKHO;
                Dlnhanvien.SelectedValue = objKho.MANV;
                txtdiachi.Value = objKho.DIACHI;
                txtsdtcodinh.Value = objKho.SDTB;
                txtsdtdidong.Value = objKho.DTDD;
                txtnguoilh.Value = objKho.NGUOILH;
                txtfax.Value = objKho.FAX;
                txtghichu.Value = objKho.GHICHU;
                if (objKho.TINHTRANG == true)
                {
                    rdhd.Checked = true;
                }
                else
                {
                    rdKohd.Checked = true;
                }
            }
            
        }
        /// <summary>
        /// Load Nhân viên
        /// </summary>
        public void LoadNhanVien()
        {
            NHANVIENRepository repository = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunitfwork);
            IQueryable<NHANVIEN> objNhanvien = repository.All();
            Dlnhanvien.DataSource = objNhanvien;
            Dlnhanvien.DataValueField = "MANV";
            Dlnhanvien.DataTextField = "TENNV";
            Dlnhanvien.DataBind();
            
        }
        public void capnhap()
        {
            bool iscapnhap = true;

            string strid = "" + Request["MAKHO"];
            KHORepository repository = new KHORepository(new EFRepository<KHO>(), iunitfwork);
            KHO objKho = null;
            if (!string.IsNullOrEmpty(strid))
            {
                iscapnhap = false;
                objKho = repository.GetById(strid);
            }
            else
            {
                iscapnhap = true;
                objKho = new KHO();
            }
            if (string.IsNullOrEmpty(txtmakho.Value) || Dlnhanvien.SelectedValue.Equals("0"))
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert('Vui lòng nhập đầy đủ')</script>");
                return;
            }
            objKho.MAKHO = txtmakho.Value;
            objKho.MANV = Dlnhanvien.SelectedValue;
            objKho.TENKHO = txttenkho.Value;
            objKho.DIACHI = txtdiachi.Value;
            objKho.SDTB = txtsdtcodinh.Value;
            objKho.DTDD = txtsdtdidong.Value;
            objKho.NGUOILH = txtnguoilh.Value;
            objKho.FAX = txtfax.Value;
            objKho.GHICHU = txtghichu.Value;
            if (rdhd.Checked)
            {
                objKho.TINHTRANG = true;
            }
            try
            {
                if (iscapnhap)
                {
                    repository.Add(objKho);
                }
                repository.Save();
            }
            catch (Exception ex)
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert('Vui lòng nhập đầy đủ')</script>");
                Response.Redirect("KhoAdd.aspx?MAKHO=" + strid);
            }
        }
        protected void lbtncapnhap_Click(object sender, EventArgs e)
        {
            capnhap();
        }

        protected void ltbnthemmoi_Click(object sender, EventArgs e)
        {
            txtdiachi.Value = "";
            txtfax.Value = "";
            txtghichu.Value = "";
            txtmakho.Value = "";
            txtnguoilh.Value = "";
            txtsdtcodinh.Value = "";
            txtsdtdidong.Value = "";
            txttenkho.Value = "";
            Dlnhanvien.SelectedValue = "0";
            Response.Redirect("KhoAdd.aspx");
        }
    }
}